package cn.wizzer.app.web.modules.controllers.platform.match;


import cn.wizzer.app.match.modules.services.MatchService;
import cn.wizzer.app.user.modules.models.vo.MatchLives;
import cn.wizzer.app.user.modules.services.LiveBroadcastService;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import java.util.Collection;

@IocBean
@At("/platform/match/match")
public class MatchController {

    @Reference
    @Inject
    private MatchService matchService;
    @Reference
    @Inject
    private LiveBroadcastService liveService;


    @At("/")
    @Ok("beetl:/platform/match/index.html")
    @RequiresPermissions("user.match")
    public void index() {
    }

    /**
     * 根据分类信息查询赛事信息
     *
     * @param code String
     * @return Object
     */
    @At("/data")
    @Ok("json:full")
    @RequiresPermissions("user.match.list")
    public Object data(@Param("code") String code, @Param("searchDate") String[] searchDate,@Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {

        Long startTime = null;
        Long endTime = null;
        if (null != searchDate && searchDate.length != 0) {
            startTime = Times.d2TS(Times.D(searchDate[0])) * 1000;
            endTime = Times.d2TS(Times.D(searchDate[1])) * 1000;
        }
        Pagination pagination = matchService.listByCode(code, startTime, endTime, pageNumber, pageSize);
        return Result.success(pagination);
    }

    @At("/edit/?")
    @Ok("json")
    public Object edit(@Param("id") String id) {
        return Result.success(matchService.hget(id));
    }

    @At("/editDo")
    @Ok("json")
    public Object editDo(@Param("..") JSONObject param) {
        MatchLives matchLives = param.getJSONObject("matchLives").toJavaObject(MatchLives.class);
        //设置直播数据标签
        liveService.updateLabelsByVideoId(matchLives.getMatch().getVideoId(),matchLives.getMatch().getLabels());
        //标签同步
        matchLives.getAnchors().forEach(anchor->{anchor.setLabels(matchLives.getMatch().getLabels());});
        //更新赛程缓存
        matchService.updateByVideoId(matchLives);
        return Result.success();
    }
    /**
     * 查询所有分类列表
     *
     * @return Object
     */
    @At("/categoryList")
    @Ok("json")
    public Object categoryList() {
        try {
            Collection collection = matchService.categoryList();
            return Result.success().addData(collection);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

}
